package spring.my.suveng.mybatis.原生dao实现.dao.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import spring.my.suveng.mybatis.原生dao实现.dao.UserMapper;
import spring.my.suveng.mybatis.原生dao实现.user.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * author Veng Su
 * email suveng@163.com
 * date 2018/8/16 11:11
 */
public class UserMapperImpl implements UserMapper {
    static InputStream is;
    static SqlSessionFactory sqlSessionFactory;
    public UserMapperImpl() throws IOException {
        preConfig();
    }


    private static void preConfig() throws IOException {
        is = Resources.getResourceAsStream("spring/my/suveng/mybatis/原生dao实现/config/mybatis.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

    }
    @Override
    public List<User> selectUserById(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> user1=sqlSession.selectList("selectUserById",user);
        sqlSession.commit();
        sqlSession.close();
        return user1;
    }
    @Override
    public List<User> selectUsers() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users=sqlSession.selectList("selectUsers");
        sqlSession.commit();
        sqlSession.close();
        return users;
    }
}
